//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using ResourceLabel = CommunityServer.ControlPanel.Controls.ResourceLabel;
using CSFolders = CommunityServer.Files.Components.Folders;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using Style = CommunityServer.Controls.Style;

namespace CommunityServer.ControlPanel.FileAdmin
{
	/// <summary>
	/// Summary description for FolderGroupForm.
	/// </summary>
	public class FolderGroupForm : BaseFilesAdminPage
	{
		#region Child Controls

		protected Style ControlPanelStyle;
		protected FormLabel tt;
		protected FormLabel Formlabel1;
		protected MPContent bcr;
		protected MPContainer MPContainer;
		protected ResourceLabel Resourcelabel2;
		protected RequiredFieldValidator ForumGroupNameValidator;
		protected TextBox GroupName;
		protected TextBox GroupDesc;

		#endregion
	
		private int groupID;
		private bool IsEdit = false;
		protected ResourceLinkButton Save;
		private ApplicationType applicationType = ApplicationType.FileGallery;

		private bool IsNew
		{
			get{return groupID <= 0;}
		}


		private void Page_Load(object sender, EventArgs e)
		{
			CSContext context = CSContext.Current;
			groupID = context.GetIntFromQueryString("GroupID", -1);

			if(!Page.IsPostBack)
			{
				BindData();
			}

			if(IsNew)
			{
				Head.AddTitle(CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_FilesAdmin_FolderGroup_Title_New"), this.Context);
			}
			else
			{
				Head.AddTitle(CommunityServer.ControlPanel.Components.ResourceManager.GetString("CP_FilesAdmin_FolderGroup_Title_Existing"), this.Context);
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			Save.Click += new EventHandler(Save_Click);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		protected void BindData() 
		{
			
			Group group = CurrentSectionGroup();
			if( group != null ) 
			{
				LoadGroup( group );
			}
		}

		protected Group CurrentSectionGroup()
		{
			CSContext cntx = CSContext.Current;
			if(cntx.User.IsFileAdministrator)
				return CSFolders.GetFolderGroup(this.groupID, false, true);
			else
				throw new CSException( CSExceptionType.AdministrationAccessDenied );
		}

		protected virtual void LoadGroup( Group group ) 
		{
			GroupName.Text = group.Name;
			GroupDesc.Text = group.Description;
		}
		

		private void Save_Click(object sender, EventArgs e)
		{
			if(Page.IsValid)
			{
				Group group = CurrentSectionGroup();

				if(group == null)
				{
					group = new Group(GroupName.Text.Trim());
					group.ApplicationType = applicationType;
				}

				if( SaveGroup( group ) )
				{
					Modal.ClosePage(Page) ;
				}

			}
			
		}

		protected virtual bool SaveGroup( Group group ) 
		{
			group.Name = GroupName.Text.Trim();
			group.Description = GroupDesc.Text.Trim();

			foreach(Group g in GetGroups())
			{
				if((g.Name.ToLower() == group.Name.ToLower() ) && (g.GroupID != group.GroupID))
				{
					ForumGroupNameValidator.IsValid = false;
					return false;
				}
			}

			if(Page.IsValid)
			{
				if(IsNew)
					Groups.AddGroup( group ); 
				else
					Groups.UpdateGroup( group );
				IsEdit = true;
				GetGroups();
				return true;
			}
			else
				return false;
		}

		protected ArrayList GetGroups()
		{
			return CSFolders.GetFolderGroups(true, true, IsEdit);
		}


	}
}
